import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
	 
	
public class SteamersUI extends Application
{
	 private BorderPane root;
	 
     final TextField userTextField = new TextField();

	     @Override
	     public void start(Stage primaryStage) throws Exception
	     {
	         root = new BorderPane();       
	         root.setTop(getTop());
	         root.setRight(getRightHBox());
	         root.setBottom(getFooter());
	         root.setLeft(getLeftHBox());
	         root.setCenter(getCenterPane());

	         Scene scene = new Scene(root, 900, 500); 
	         primaryStage.setTitle("Steamers");
	         primaryStage.setScene(scene);
	         primaryStage.show();   
	     }
	     
	     private VBox getTop()
	     {
	    	 VBox topVBox = new VBox();

	         HBox topHBox = new HBox(20);
	         topHBox.setPadding(new Insets(5));
	         topHBox.setAlignment(Pos.CENTER);
	         topHBox.setStyle("-fx-background-color: #585858");
	         
	         Label steamName = new Label("Steam Name:");
	         steamName.setStyle("-fx-text-fill: white");
	         
	         topHBox.getChildren().addAll(steamName,userTextField);       
	         topVBox.getChildren().addAll(new Separator(), topHBox);
	         topVBox.setStyle("-fx-background-color: #585858");
	 
	         return topVBox;
	     }

	     private HBox getRightHBox()
	     {
	         HBox rightHBox = new HBox();

	         VBox rightVBox = new VBox(0);
	         rightVBox.setPadding(new Insets(0, 0, 0, 0));
	         rightVBox.setAlignment(Pos.CENTER);

	         return rightHBox;
	     }

	     private HBox getLeftHBox()
	     {
	         HBox leftHBox = new HBox();
	         leftHBox.setStyle("-fx-background-color: #585858");
	         
	         VBox leftVBox = new VBox(10);
	         leftVBox.setPadding(new Insets(10));
	         leftVBox.setStyle("-fx-background-color: #585858");

	         Text text = new Text("Navigation");
	         text.setFont(Font.font("Helvetica", FontWeight.BOLD,20));
	         text.setFill(Color.WHITE);

	         VBox vboxText = new VBox(10);

	         for (int i = 1; i >= 10; i++)
	         {
	             vboxText.getChildren().add(new Text("Category " + i));
	         }       
	         vboxText.setTranslateX(10);
	         
	         Button btn = new Button("User Summary");
	         
	         btn.setOnAction(new EventHandler<ActionEvent>() {
	        	 
	        	    @Override
	        	    public void handle(ActionEvent e) {
	        	    	System.out.println("User Summary");
	        	        try
						{
	        	        	SteamUser steamUser = new SteamUser();
	        	        	steamUser.setSteamID(userTextField.getText());

							String userSummaryURL = SteamXMLParsers.createUserSummaryURL(steamUser);
							String xml = SteamXMLParsers.returnXML(userSummaryURL);
							
							StackPane stackPane = new StackPane();
					        stackPane.setAlignment(Pos.CENTER);

					        stackPane.getChildren().addAll(new Text(xml));
					        root.setCenter(stackPane);
						}
						catch (Exception e1)
						{
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
	        	    }
	        	});
	         
	         Button btn2 = new Button("Friends List");
	         
	         btn2.setOnAction(new EventHandler<ActionEvent>() {
	        	 
	        	    @Override
	        	    public void handle(ActionEvent e) {
	        	    	try
						{
	        	    		SteamUser steamUser = new SteamUser();
	        	        	steamUser.setSteamID(userTextField.getText());

	        	        	System.out.println("SteamID: "+steamUser.getSteamID());
	        	        	
							String friendsListURL = SteamXMLParsers.createFriendsListURL(steamUser);
							String xml = SteamXMLParsers.returnXML(friendsListURL);
							
							System.out.println("Friend List XML: "+xml);
							
							SteamUser[] friends = SteamXMLParsers.friendsListParse(xml);
									
							TilePane tilePane = new TilePane(Orientation.VERTICAL);
					        tilePane.setAlignment(Pos.CENTER);
					        tilePane.setHgap(5);
					        tilePane.setVgap(5);
					        tilePane.setPrefHeight(100);
					        tilePane.setStyle("-fx-background-color: #585858");
					        VBox.setMargin(tilePane, new Insets(10));

					        System.out.println("Friends Length: "+friends.length);

					        for(int f = 0;f<friends.length-1;f++)
					        {
					        	Text handle = new Text(friends[f].getHandle());
					        	int userStatus = friends[f].getStatus();
					        	
					        	switch(userStatus)
					        	{
					        		case 0:
					        			handle.setFill(Color.RED);
					        			break;
					        		case 1:
					        			handle.setFill(Color.GREEN);
					        			break;
					        		case 2:
					        			handle.setFill(Color.ORANGE);
					        			break;
					        		case 3:
					        			handle.setFill(Color.YELLOW);
					        			break;
					        		case 4:
					        			handle.setFill(Color.BLUE);
					        			break;
					        		case 5:
					        			handle.setFill(Color.BEIGE);
					        			break;
					        		case 6:
					        			handle.setFill(Color.OLIVE);	
					        			break;
					        		default:
					        			handle.setFill(Color.WHITE);
					        			break;
					        	}
					        	
						        tilePane.getChildren().addAll(handle,new ImageView(friends[f].getAvatar()));
					        }

					        root.setCenter(tilePane);
						}
						catch (Exception e1)
						{
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
	        	    }
	        	});
	         
	         leftVBox.getChildren().addAll(text, vboxText, btn, btn2);       
	         leftHBox.getChildren().addAll(leftVBox, new Separator(Orientation.VERTICAL));
	         
	         return leftHBox;
	     }

	     private VBox getFooter()
	     {
	         VBox footVBox = new VBox();

	         HBox footHBox = new HBox(0);
	         footHBox.setPadding(new Insets(0));
	         footHBox.setAlignment(Pos.CENTER);

	         return footVBox;
	     }

	     private StackPane getCenterPane()
	     {
	         StackPane stackPane = new StackPane();
	         stackPane.setAlignment(Pos.CENTER);
	         stackPane.setStyle("-fx-background-color: #585858");

	         Rectangle rec = new Rectangle(200, 200);
	         rec.setFill(Color.GRAY);
	         rec.setStyle("-fx-border-style: inside");
	         rec.widthProperty().bind(stackPane.widthProperty().subtract(50));
	         rec.heightProperty().bind(stackPane.heightProperty().subtract(50));

	         stackPane.getChildren().addAll(rec);
	         
	         return stackPane;
	     }
	 
	     public static void main(String[] args)
	     {
	         Application.launch(args);
	     }

}


